package com.feilong.edgeAI.remind.room.ui;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ConvertUtils;
import com.feilong.edgeAI.R;
import com.feilong.edgeAI.adapter.RemindAdapter;
import com.feilong.edgeAI.adapter.SpacingItemDecoration;
import com.feilong.edgeAI.remind.room.adapter.RemindListAdapter;
import com.feilong.edgeAI.remind.room.bean.RemindBean;
import com.feilong.edgeAI.remind.room.db.RemindDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SingleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SingleFragment extends Fragment implements EditDialog.OnRefreshDataListener{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private View view;
    private RecyclerView recyclerView;
    private RemindListAdapter mAdapter;
    private List<RemindBean> remindBeanList = new ArrayList<>();

    private EditDialog dialog ;
    public SingleFragment() {
        // Required empty public constructor
    }

    public static SingleFragment newInstance(String param1, String param2) {
        SingleFragment fragment = new SingleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }


    public void initData() {
        remindBeanList = RemindDatabase.getDefault(getActivity().getApplicationContext()).getRemindDao().loadRemindByModes("0");
        mAdapter = new RemindListAdapter(remindBeanList);
        recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new RemindListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RemindBean remindBean) {
                Bundle mBundle = new Bundle();
                mBundle.putParcelable("RemindBean", remindBean);
                dialog.show(getActivity(), mBundle, "edit");
                dialog.setOnRefreshDataListener(new EditDialog.OnRefreshDataListener() {
                    @Override
                    public void onRefresh() {
                        //cancelAlarm();
                        initData();

                    }
                });
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_single, container, false);

        dialog = new EditDialog();
        dialog.setOnRefreshDataListener(this);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity().getApplicationContext(), 1));
        remindBeanList = RemindDatabase.getDefault(getActivity().getApplicationContext()).getRemindDao().getRemindAll();
        mAdapter = new RemindListAdapter(remindBeanList);
        initData();

        return view;
    }


    @Override
    public void onRefresh() {
        initData();
    }
}